package com.quanshu.exchange.ui.gui.message.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.entity.GroupLabelEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 群分类
 * Created by xiniu003 on 2017/12/8.
 */

public class GroupLabelActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.comment_group_classify)
    LinearLayout commentGroupClassify;
    @BindView(R.id.group_label_RecyclerView)
    RecyclerView recyclerView;

    private SingleAdapter<GroupLabelEntity> adapter;
    private List<GroupLabelEntity> dataList;

    private GroupLabelEntity groupLabelEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_classify);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initData();
        initListener();
    }

    private void initIntent() {
        groupLabelEntity = (GroupLabelEntity) getIntent().getSerializableExtra("GroupLabelEntity");
    }

    private void initView() {

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new SingleAdapter<>(new DataDisplayer<GroupLabelEntity>(this, R.layout.item_group_label) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<GroupLabelEntity> wrapper, GroupLabelEntity item, int position) {
                holder.setText(R.id.group_label_name, item.getLabel_name());
                holder.setImage(R.id.group_label_img, item.getLabel_image());
                if (groupLabelEntity == null) {
                    holder.setVisibility(R.id.group_label_check, View.GONE);
                } else {
                    if (groupLabelEntity.getId() == item.getId()) {
                        holder.setVisibility(R.id.group_label_check, View.VISIBLE);
                    } else {
                        holder.setVisibility(R.id.group_label_check, View.GONE);
                    }

                }
                holder.setOnClickListener(view -> {
                    groupLabelEntity = item;
                    adapter.notifyDataSetChanged();
                });
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(commentGroupClassify, aVoid -> doTask());

    }

    private void initData() {

        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .getGroupLabelList()
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<GroupLabelEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<GroupLabelEntity>> response) {
                        dataList = new ArrayList<>();
                        if (response.getInfo() != null && response.getInfo().size() > 0) {
                            for (GroupLabelEntity entity : response.getInfo()) {
                                if (entity.getId() == 1) {
                                    entity.setLabel_image(R.drawable.label_study_friend);
                                } else if (entity.getId() == 2) {
                                    entity.setLabel_image(R.drawable.label_exchange_jiaoliu);
                                } else if (entity.getId() == 3) {
                                    entity.setLabel_image(R.drawable.label_liveing_xiuxian);
                                } else if (entity.getId() == 4) {
                                    entity.setLabel_image(R.drawable.label_interests_aihao);
                                } else if (entity.getId() == 5) {
                                    entity.setLabel_image(R.drawable.label_brand_product);
                                } else if (entity.getId() == 6) {
                                    entity.setLabel_image(R.drawable.label_outdoors_exercise);
                                } else if (entity.getId() == 7) {
                                    entity.setLabel_image(R.drawable.label_game);
                                } else if (entity.getId() == 8) {
                                    entity.setLabel_image(R.drawable.label_else_image);
                                }
                                dataList.add(entity);
                            }
                            adapter.setData(dataList);
                        }
                    }
                });
    }

    private void doTask() {
        if (groupLabelEntity == null) {
            ToastHelper.toast("请选择群类型！");
            return;
        }
        Intent intent = getIntent();
        intent.putExtra("GroupLabelEntity", groupLabelEntity);
        setResult(RESULT_OK, intent);
        finish();
    }


}
